<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Artikel extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->_data["menuHL"] = "Artikel";
        $this->addBreadCrumb("Artikel", site_url("artikel"));
    }

    protected function _renderPage($page) {
        $this->_data["page"] = $page;
        parent::_renderPage("artikel/base");
    }

    public function index() {
        redirect("artikel/berita");
    }

    public function updateApproveArtikel() {
        $this->load->model("Artikel_model");
        $this->load->model("Content_model");
        $authorized = Array("super_admin");

        $Obj = $this->Artikel_model->findById($this->input->post("id"));
        if ($Obj != NULL) {
            switch ($Obj->type) {
                case Artikel_model::TYPE_BERITA :
                    $authorized = Array("berita_auditor", "master_admin");
                    break;
                case Artikel_model::TYPE_KABAR_RELAWAN :
                    $authorized = Array("kr_auditor", "master_admin");
                    break;
                case Artikel_model::TYPE_PUBLIK :
                    $authorized = Array("pj_auditor", "master_admin");
                    break;
            }
        }

        if ($this->isAuth($authorized)) {
            if ($this->Content_model->updateById($Obj->content_id, Array(
                        "title" => $this->input->post("title"),
                        "text" => $this->input->post("text")
                    ))) {
                if ($this->Artikel_model->updateById($Obj->id, Array("is_approved" => true))) {
                    echo 1;
                    exit;
                }
            }
        }

        echo 0;
    }

    public function deleteArtikel() {
        $authorized = Array("super_admin");
        $this->load->model("Artikel_model");
        $Obj = $this->Artikel_model->findById($this->input->post("id"));
        if ($Obj != NULL) {
            switch ($Obj->type) {
                case Artikel_model::TYPE_BERITA :
                    $authorized = Array("berita_auditor", "master_admin");
                    break;
                case Artikel_model::TYPE_KABAR_RELAWAN :
                    $authorized = Array("kr_auditor", "master_admin");
                    break;
                case Artikel_model::TYPE_PUBLIK :
                    $authorized = Array("pj_auditor", "master_admin");
                    break;
            }
        }
        if ($this->isAuth($authorized)) {
            $this->load->model("Artikel_model");
            if ($this->Artikel_model->deleteById($this->input->post("id"))) {
                echo 1;
                exit;
            }
        }
        echo 0;
    }

    public function saveHLArtikel() {
        $authorized = Array("super_admin");
        $this->load->model("Artikel_model");
        $Obj = $this->Artikel_model->findById($this->input->post("id"));
        if ($Obj != NULL) {
            switch ($Obj->type) {
                case Artikel_model::TYPE_BERITA :
                    $authorized = Array("berita_auditor");
                    break;
                case Artikel_model::TYPE_KABAR_RELAWAN :
                    $authorized = Array("kr_auditor");
                    break;
                case Artikel_model::TYPE_PUBLIK :
                    $authorized = Array("pj_auditor");
                    break;
            }
        }
        if ($this->isAuth($authorized)) {
            $id = $this->input->post("id");
            return $this->Artikel_model->makeHL($id);
        }
    }

    public function berita($beritaId = -1) {
        $this->addBreadCrumb("Berita PS", site_url("artikel/berita"));

        if ($beritaId < 0) {
            $this->_data["pageTitle"] = "Berita Perdesaan Sehat";
            $this->load->model("Artikel_model");

            // hlList
            $hlBerita = $this->Artikel_model->beritaHL();
            $this->_data["hlBerita"] = $hlBerita;

            // pics
            $picList = $this->Artikel_model->beritaRecentPics();
            $this->_data["picList"] = $picList;

            // beritaList (page 1)
            $beritaPage1 = $this->Artikel_model->beritaRecent(0);
            $this->_data["beritaPage1"] = $beritaPage1;

            $this->_renderPage("berita");
        } else {
            $this->load->model("Artikel_model");
            $berita = $this->Artikel_model->beritaById($beritaId, true, 2);
            if ($berita != NULL && ($berita->is_approved || ($berita->content_id->poster_id->id == $this->_loggedUser->id))) {
                $this->addBreadCrumb($berita->content_id->title, site_url("artikel/berita/" . $beritaId));
                $this->_data["berita"] = $berita;
                $this->_data["isArticle"] = true;
                $this->_data["titleFB"] = $berita->content_id->title;
                $this->_data["imageFB"] = $berita->content_id->hlPicSrc;
                $this->_data["descFB"] = $berita->content_id->short;
                $this->_renderPage("berita/detail");
            } else {
                redirect("home/error");
            }
        }
    }

    public function kr($artikelId = -1) {
        $this->_data["pageTitle"] = "Kabar Relawan";
        $this->addBreadCrumb("Kabar Relawan", site_url("artikel/kr"));

        if ($artikelId < 0) {
            $this->load->model("Artikel_model");

            // hlList
            $hlList = $this->Artikel_model->krHL();
            $this->_data["hlList"] = $hlList;

            // relawanList
            $this->load->model("User_model");
            $this->_data["relawanList"] = $this->User_model->getListRelawan("rand", 0, 4);

            // pics
            $picList = $this->Artikel_model->krRecentPics();
            $this->_data["picList"] = $picList;

            $this->_renderPage("kr");
        } else {
            $this->load->model("Artikel_model");
            $artikel = $this->Artikel_model->krById($artikelId, true, 3);
            if ($artikel != NULL && ($artikel->is_approved || ($artikel->content_id->poster_id->id == $this->_loggedUser->id))) {
                $this->addBreadCrumb($artikel->content_id->title, site_url("artikel/kr/" . $artikelId));
                $this->_data["artikel"] = $artikel;
                $this->_data["isArticle"] = true;
                $this->_data["titleFB"] = $artikel->content_id->title;
                $this->_data["imageFB"] = $artikel->content_id->hlPicSrc;
                $this->_data["descFB"] = "Oleh " . $artikel->content_id->poster_id->display_name . " - " . $artikel->content_id->short;
                $this->_renderPage("kr/detail");
            } else {
                redirect("home/error");
            }
        }
    }

    public function pj($artikelId = -1) {
        $this->addBreadCrumb("Jurnalisme Publik", site_url("artikel/pj"));
        $this->_data["pageTitle"] = "Jurnalisme Publik";

        if ($artikelId < 0) {
            $this->load->model("Artikel_model");

            // hlList
            $hlList = $this->Artikel_model->pjHL();
            $this->_data["hlList"] = $hlList;

            // pics
            $picList = $this->Artikel_model->pjRecentPics();
            $this->_data["picList"] = $picList;

            $this->_renderPage("pj");
        } else {
            $this->load->model("Artikel_model");
            $artikel = $this->Artikel_model->pjById($artikelId, true, 2);
            if ($artikel != NULL && ($artikel->is_approved || ($artikel->content_id->poster_id->id == $this->_loggedUser->id))) {
                $this->addBreadCrumb($artikel->content_id->title, site_url("artikel/pj/" . $artikelId));
                $this->_data["artikel"] = $artikel;
                $this->_data["isArticle"] = true;
                $this->_data["titleFB"] = $artikel->content_id->title;
                $this->_data["imageFB"] = $artikel->content_id->hlPicSrc;
                $this->_data["descFB"] = $artikel->content_id->short;
                $this->_renderPage("pj/detail");
            } else {
                redirect("home/error");
            }
        }
    }

    public function saveBerita() {
        $authorized = Array("berita_contributor", "master_admin");
        if ($this->isAuth($authorized)) {
            $pics = Array();
            $this->load->model("Image_model");
            for ($i = 0; $i < 5; $i++) {
                $tmp = $this->input->post("pic" . $i);
                if ($tmp != NULL) {
                    array_push($pics, $this->Image_model->create($tmp, "", Array()));
                }
            }
            $this->load->model("Artikel_model");
            $beritaId = $this->Artikel_model->createBerita(
                    $this->input->post("judul"), $this->input->post("konten"), $this->input->post("sumber"), Array(), $pics
            );
            echo $beritaId;
            exit;
        } else {
            
        }
    }

    public function saveKR() {
        $authorized = Array("kr_contributor", "master_admin");
        if ($this->isAuth($authorized)) {
            $pics = Array();
            $this->load->model("Image_model");
            for ($i = 0; $i < 5; $i++) {
                $tmp = $this->input->post("pic" . $i);
                if ($tmp != NULL) {
                    array_push($pics, $this->Image_model->create($tmp, "", Array()));
                }
            }
            $this->load->model("Artikel_model");
            $artikelId = $this->Artikel_model->createKR(
                    $this->input->post("judul"), $this->input->post("konten"), Array(), $pics
            );
            echo $artikelId;
            exit;
        } else {
            
        }
    }

    public function savePJ() {
        $authorized = Array("pj_contributor", "master_admin");
        if ($this->isAuth($authorized)) {
            $pics = Array();
            $this->load->model("Image_model");
            for ($i = 0; $i < 5; $i++) {
                $tmp = $this->input->post("pic" . $i);
                if ($tmp != NULL) {
                    array_push($pics, $this->Image_model->create($tmp, "", Array()));
                }
            }
            $this->load->model("Artikel_model");
            $artikelId = $this->Artikel_model->createPJ(
                    $this->input->post("judul"), $this->input->post("konten"), Array(), $pics
            );
            echo $artikelId;
            exit;
        } else {
            
        }
    }

}
